package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.dao.Import;
import com.example.dao.SearchGoods;
import com.example.entity.*;
import com.example.service.VHnyyVenderService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/link")
public class VHnyyVenderController {
    @Autowired
    private VHnyyVenderService vHnyyVenderService;
    @GetMapping("/getParty")
    public Result getParty(HttpServletRequest request, @RequestParam String name,@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<VHnyyVender> party = vHnyyVenderService.getParty(comPartyId,name,pageNum,pageSize);
        return Result.success(party);
    }
    @PostMapping("/getGoods")
    public Result getGoods(HttpServletRequest request, @RequestBody SearchGoods searchGoods){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<VHnyyVenderGoodsQuery> party = vHnyyVenderService.getGoods(comPartyId,searchGoods);
        return Result.success(party);
    }
    @GetMapping("/getClient")
    public Result getClient(HttpServletRequest request, @RequestParam String name,@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<VHnyyVender> party = vHnyyVenderService.getClient(comPartyId,name,pageNum,pageSize);
        return Result.success(party);
    }
    @GetMapping("/getClientList")
    public Result getClientList(HttpServletRequest request,@RequestParam String name,@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<SysClientPriceSets> party = vHnyyVenderService.getClientList(comPartyId,name,pageNum,pageSize);
        return Result.success(party);
    }
    @PostMapping("/addPolicy")
    public Result addPolicy(HttpServletRequest request,@RequestBody HnyyPurRebateList hnyyPurRebateList){
        String user = (String) request.getAttribute("saler");
        String comPartyId = (String) request.getAttribute("comPartyId");
        hnyyPurRebateList.setCreater(user);
        hnyyPurRebateList.setCreateDate(new java.util.Date());
        hnyyPurRebateList.setInvOwner(comPartyId);
        hnyyPurRebateList.setOpFlag("NEW");
        System.out.println("hnyyPurRebateList===+>"+JSONObject.toJSONString(hnyyPurRebateList));
        vHnyyVenderService.addPolicy(hnyyPurRebateList);
        return Result.ok();
    }
    @GetMapping("/getCheck")
    public Result getCheck(HttpServletRequest request,@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam (required = false) String key,
                           @RequestParam (required = false) String vender,
                           @RequestParam (required = false) String goodsDesc,
                           @RequestParam (required = false) String creater,
                           @RequestParam (required = false) String startDate,
                           @RequestParam (required = false) String endDate
                           ){
        System.out.println("key=========>"+key+startDate+endDate);
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<VHnyyVenderGoodsQuery> check = vHnyyVenderService.getCheck(pageNum, pageSize,key,comPartyId,vender,goodsDesc,creater,startDate,endDate);
        return Result.success(check);
    }

    @GetMapping("/getTem")
    public Result getTem(@RequestParam Integer pageNum,
                         @RequestParam Integer pageSize){
        PageInfo<VHnyyPurRebateList> tem = vHnyyVenderService.getTem(pageNum, pageSize);
        return Result.success(tem);
    }
    @PostMapping("/updatePolicy")
    public Result updatePolicy(HttpServletRequest request,@RequestBody HnyyPurRebateList hnyyPurRebateList){
        String saler = (String) request.getAttribute("saler");
        hnyyPurRebateList.setChecker(saler);
        hnyyPurRebateList.setCheckDate(new Date());
        if (hnyyPurRebateList.getOpFlag().equals("1")){
            hnyyPurRebateList.setOpFlag("FINISH");
        }else if (hnyyPurRebateList.getOpFlag().equals("0")){
            hnyyPurRebateList.setOpFlag("CANCEL");
        }else{
            hnyyPurRebateList.setOpFlag("NEW");
        }
        vHnyyVenderService.updatePolicy(hnyyPurRebateList);
        return Result.ok();
    }
    @GetMapping("/getPolicy")
    public Result getPolicy(HttpServletRequest request,@RequestParam String comGoodsId,String sysVenderGoodsRefId){
        String comPartyId = (String) request.getAttribute("comPartyId");
        HnyyPurRebateList party = vHnyyVenderService.getPolicy(comGoodsId,sysVenderGoodsRefId,comPartyId);
        return Result.success(party);
    }
    @GetMapping("/getClientById")
    public Result getClientById(HttpServletRequest request,@RequestParam String clientId){
        String comPartyId = (String) request.getAttribute("comPartyId");
        List<VHnyyVender> party = vHnyyVenderService.getClientById(comPartyId,clientId);
        return Result.success(party);
    }
    @GetMapping("/getSysClientPriceSets")
    public Result getVenderList(HttpServletRequest request,@RequestParam String sysClientPriceSetsList) {
        // 拆分字符串
        List<String> ids = Arrays.asList(sysClientPriceSetsList.split(","));
        String comPartyId = (String) request.getAttribute("comPartyId");
        List<String> names = vHnyyVenderService.getVenderList(comPartyId,ids);

        return Result.success(names);
    }
    @PostMapping("/getImport")
    public Result getImport(HttpServletRequest request, @RequestBody Import importName){
        String comPartyId = (String) request.getAttribute("comPartyId");
        System.out.println("12121"+JSONObject.toJSONString(importName));
        VHnyyVenderGoodsQuery party = vHnyyVenderService.getImport(comPartyId,importName);
        return Result.success(party);
    }
    // 批量导入临时存入数据库
    @PostMapping("/addTem")
    public Result addTem(HttpServletRequest request, @RequestBody HnyyPurRebateList hnyyPurRebateList) {
        System.out.println("hnyyPurRebateList"+JSONObject.toJSONString(hnyyPurRebateList));
        String user = (String) request.getAttribute("saler");
        String comPartyId = (String) request.getAttribute("comPartyId");
        hnyyPurRebateList.setCreater(user);
        hnyyPurRebateList.setCreateDate(new java.util.Date());
        hnyyPurRebateList.setInvOwner(comPartyId);
        hnyyPurRebateList.setOpFlag("NEW");
        System.out.println("hnyyPurRebateList===+>"+JSONObject.toJSONString(hnyyPurRebateList));
        vHnyyVenderService.saveBatch(hnyyPurRebateList);
        return Result.ok();
    }
    @GetMapping("/updateTem")
    public Result updateTem(HttpServletRequest request, @RequestParam String id, @RequestParam String opFlag) {
        int i = vHnyyVenderService.updateTem(id, opFlag);
        return i > 0 ? Result.ok() : Result.error();
    }

    @GetMapping("/getVenderList")
    public Result getVenderList(HttpServletRequest request,@RequestParam String name,@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize){
        String comPartyId = (String) request.getAttribute("comPartyId");
        PageInfo<ComVender> party = vHnyyVenderService.getVenderList(comPartyId,name,pageNum,pageSize);
        return Result.success(party);
    }

    @GetMapping("/getGoodsList")
    public Result getGoodsList(@RequestParam String name,@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize){
        PageInfo<ComGoods> party = vHnyyVenderService.getGoodsList(name,pageNum,pageSize);
        return Result.success(party);
    }

}
